//
//  YXPopupListView.m
//  YXPopList_Example
//
//  Created by GMS on 2020/5/15.
//  Copyright © 2020 Arthur. All rights reserved.
//

#import "YXPopupListView.h"
#import "YXPopupListTVCell.h"

@interface YXPopupListView ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

// MARK: 模型数组，只是模拟
@property (nonatomic,strong) NSArray *modelArray;


@end

@implementation YXPopupListView

// MARK: view初始化
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
       NSString *className = NSStringFromClass([self class]);
       self = [[[NSBundle mainBundle] loadNibNamed:className owner:self options:nil] lastObject];
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [window addSubview:self];
    }
    return self;
}

// MARK: 单例
+ (YXPopupListView *)shareInstance {
    
    static YXPopupListView *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[YXPopupListView alloc] init];
    });
    return  _instance;
}


// MARK: 根据传过来的数据和尺寸来显示列表位置和大小
+ (instancetype)showWithArray:(NSArray *)dataArray withTableViewRect:(CGRect)rect andCallBack:(CallBack)callBack {
    
    YXPopupListView *popupListView = [YXPopupListView shareInstance];
    popupListView.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
    popupListView.hidden = NO;
    popupListView.callBack = callBack;
    
    popupListView.tableView.frame = rect;
    [popupListView.tableView reloadData];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window bringSubviewToFront:popupListView];
    return popupListView;
}


// MARK: section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// MARK: 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 20;
}

// MARK: 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YXPopupListTVCell *cell = [YXPopupListTVCell popupListTableViewCellWith:tableView andCommentModel:[self.modelArray objectAtIndex:indexPath.row]];
    return cell;
}

// MARK: tableViewCell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    id model = [self.modelArray objectAtIndex:indexPath.row];
    self.callBack(model);
    self.hidden = YES;
}


// MARK: 点击背景隐藏
- (IBAction)hiddenButtonClick:(UIButton *)sender {
    self.hidden = YES;
}

- (NSArray *)modelArray {
    if (!_modelArray) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"YXPopupListView.plist" ofType:nil];
        _modelArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _modelArray;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
